#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_RecordsOfReleasePermitSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'RecordsOfReleasePermit'
    
    #Read from inn by XML order

    #RecordsOfReleasePermit 
    ReleaseApprovalTypeCode = inn.get({'BOTSID':rootelement,ns1+'ReleaseApprovalTypeCode':None})
    InspectionSubjectTypeCode = inn.get({'BOTSID':rootelement,ns1+'InspectionSubjectTypeCode':None})
    StorageConfirmationNumberID = inn.get({'BOTSID':rootelement,ns1+'StorageConfirmationNumberID':None})
    ArrivalDate = inn.get({'BOTSID':rootelement,ns1+'ArrivalDate':None})
    ReleaseAcceptanceMeasure = inn.get({'BOTSID':rootelement,ns1+'ReleaseAcceptanceMeasure':None})
    ReleaseAcceptanceMeasure_measureUnitCode = inn.get({'BOTSID':rootelement,ns1+'ReleaseAcceptanceMeasure__measureUnitCode':None})
     
    #RecordsOfReleasePermit/Document
    DocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':None})
    if DocumentTypeCode == '099': DocumentTypeCode = '99'
    
    DocumentFunctionTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'FunctionTypeCode':None})
    DocumentIssueDate = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDate':None})
    DocumentIssueDateTime = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDateTime':None})
    
    #RecordsOfReleasePermit/PreviousCustomsDocument
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':None})
    
    #RecordsOfReleasePermit/Warehouse
    WarehouseCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Warehouse',ns2+'CodeID':None})
    
    #RecordsOfReleasePermit/UCR
    UCRIdentificationID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'IdentificationID':None})
    UCRMasterBLSequenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'MasterBLSequenceID':None})
    UCRHouseBLSequenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'HouseBLSequenceID':None})

    #RecordsOfReleasePermit/DutyTaxFee
    DutyTaxFeeTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'DutyTaxFee',ns2+'TypeCode':None})
    DutyTaxFeeTypeCode_codeNameText = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'DutyTaxFee',ns2+'TypeCode__codeNameText':None})
    
    #RecordsOfReleasePermit/DutyTaxFee/Payment
    DutyTaxFeePaymentTaxAssessedAmount = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'DutyTaxFee'},{'BOTSID':ns2+'Payment',ns2+'TaxAssessedAmount':None}) 
    DutyTaxFeePaymentTaxAssessedAmount_amountCurrencyID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'DutyTaxFee'},{'BOTSID':ns2+'Payment',ns2+'TaxAssessedAmount__amountCurrencyID':None})
    
    #RecordsOfReleasePermit/TradeTerm
    TradeTermConditionCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'TradeTerm',ns2+'ConditionCode':None})
    
    #RecordsOfReleasePermit/TotalItemPayment/Payment
    TotalItemPaymentTaxAssessedAmount = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'TotalItemPayment'},{'BOTSID':ns2+'Payment',ns2+'TaxAssessedAmount':None}) 
    TotalItemPaymentTaxAssessedAmount_amountCurrencyID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'TotalItemPayment'},{'BOTSID':ns2+'Payment',ns2+'TaxAssessedAmount__amountCurrencyID':None})
    
    #RecordsOfReleasePermit/AssociatedTransportDocument
    AssociatedTransportDocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'AssociatedTransportDocument',ns2+'referenceID':None})
    
    #RecordsOfReleasePermit/TransportMeans
    TransportMeansIdentificationName = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'TransportMeans',ns2+'IdentificationName':None})
    
    #RecordsOfReleasePermit/Buyer/Party
    BuyerPartyTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Buyer'},{'BOTSID':ns2+'Party',ns2+'TypeCode':None})
    BuyerPartyID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Buyer'},{'BOTSID':ns2+'Party',ns2+'ID':None})
    BuyerPartyName = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Buyer'},{'BOTSID':ns2+'Party',ns2+'Name':None})

    #RecordsOfReleasePermit/Buyer/Party/Company
    BuyerPartyCompanyName = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Buyer'},{'BOTSID':ns2+'Party'},{'BOTSID':ns2+'Company',ns2+'Name':None})
    
    #RecordsOfReleasePermit/ReleaseAcceptanceGoodsPackaging
    ReleaseAcceptanceGoodsPackagingQuantity = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'ReleaseAcceptanceGoodsPackaging',ns2+'Quantity':None})
    ReleaseAcceptanceGoodsPackagingTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'ReleaseAcceptanceGoodsPackaging',ns2+'TypeCode':None})
    
    
    out.ta_info['botskey'] = 'CUSCRA-99-' + PreviousCustomsDocumentReferenceID.strip()
        
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSCRA'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'KE'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':DocumentTypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':PreviousCustomsDocumentReferenceID})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1225':DocumentFunctionTypeCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'137','C507.2380':utils.edifactYYYYMMDDHHNN(DocumentIssueDateTime),'C507.2379':'203'})
    
    if DocumentFunctionTypeCode == '31':
        DocumentFunctionTypeCode2 = '204'
    else:
        DocumentFunctionTypeCode2 = '46'
        
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':DocumentFunctionTypeCode2,'C507.2380':utils.edifactYYYYMMDD(DocumentIssueDate),'C507.2379':'102'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'14','C517.3225':WarehouseCodeID,'C517.1131':'129','C517.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':ReleaseApprovalTypeCode,'C529.1131':'148','C529.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':InspectionSubjectTypeCode,'C529.1131':'5EA','C529.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DOC','C002.1001':'85','C503.1004':UCRIdentificationID})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DOC','C002.1001':'705','C503.1004':UCRMasterBLSequenceID})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DOC','C002.1001':'705','C503.1004':UCRMasterBLSequenceID,'C503.1366':UCRHouseBLSequenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'TAX','5283':'3','C241.5153':'CUD'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'TAX'},{'BOTSID':'MOA','C516.5025':'55','C516.5004':DutyTaxFeePaymentTaxAssessedAmount,'C516.6345':DutyTaxFeePaymentTaxAssessedAmount_amountCurrencyID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1153':'CKN','C506.1154':StorageConfirmationNumberID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'TOD','C100.4053':TradeTermConditionCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'MOA','C516.5025':'43','C516.5004':TotalItemPaymentTaxAssessedAmount,'C516.6345':TotalItemPaymentTaxAssessedAmount_amountCurrencyID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'DMS','1004':AssociatedTransportDocumentReferenceID,'1001':'705'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'TDT','8051':'20','C222.8212':TransportMeansIdentificationName})
    
    if ArrivalDate != None:out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'TDT'},{'BOTSID':'DTM','C507.2005':'178','C507.2380':utils.edifactYYYYMMDD(ArrivalDate),'C507.2379':'102'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'NAD','3035':'PR','C082.3039':BuyerPartyID,'C082.1131':BuyerPartyTypeCode,'C082.3055':'KTX','C058.3124#1':BuyerPartyCompanyName,'C058.3124#2':BuyerPartyName})

    #~Looping Start 0~999 Loop
    #RecordsOfReleasePermit/ReleaseItem
    for rff in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'ReleaseItem'}):
        ReleaseItemLineNumberID = rff.get({'BOTSID':ns1+'ReleaseItem',ns2+'LineNumberID':None})
        ReleaseItemWithholdingAcceptanceTypeCode = rff.get({'BOTSID':ns1+'ReleaseItem',ns2+'WithholdingAcceptanceTypeCode':None})
        ReleaseItemNetWeightMeasure = rff.get({'BOTSID':ns1+'ReleaseItem',ns2+'NetWeightMeasure':None})
        ReleaseItemNetWeightMeasure_measureUnitCode = rff.get({'BOTSID':ns1+'ReleaseItem',ns2+'NetWeightMeasure__measureUnitCode':None})
        ReleaseItemGoodsQuantity = rff.get({'BOTSID':ns1+'ReleaseItem',ns2+'GoodsQuantity':None}) 
        ReleaseItemGoodsQuantity_unitCode = rff.get({'BOTSID':ns1+'ReleaseItem',ns2+'GoodsQuantity__unitCode':None})
        
        #RecordsOfReleasePermit/ReleaseItem/Goods
        ReleaseItemGoodsDescription = rff.get({'BOTSID':ns1+'ReleaseItem'},{'BOTSID':ns2+'Goods',ns2+'Description':None})
        ReleaseItemGoodsGrade = rff.get({'BOTSID':ns1+'ReleaseItem'},{'BOTSID':ns2+'Goods',ns2+'Grade':None})
        
        lou = out.putloop({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'CST'})
        lou.put({'BOTSID':'CST','1496':ReleaseItemLineNumberID,'C246#1.7361':ReleaseItemWithholdingAcceptanceTypeCode,'C246#1.1131':'109','C246#1.3055':'KCS'})
        
        lou.put({'BOTSID':'CST'},{'BOTSID':'FTX','4451':'AAA','C108.4440#1':ReleaseItemGoodsDescription})
        lou.put({'BOTSID':'CST'},{'BOTSID':'FTX','4451':'AAA','C108.4440#1':ReleaseItemGoodsDescription,'C108.4440#2':ReleaseItemGoodsGrade})
        
        lou.put({'BOTSID':'CST'},{'BOTSID':'MEA','6311':'WT','C174.6411':ReleaseItemNetWeightMeasure_measureUnitCode})
        lou.put({'BOTSID':'CST'},{'BOTSID':'MEA','6311':'WT','C174.6411':ReleaseItemNetWeightMeasure_measureUnitCode,'C174.6314':ReleaseItemNetWeightMeasure})
        
        lou.put({'BOTSID':'CST'},{'BOTSID':'MEA','6311':'CT','C174.6411':ReleaseItemGoodsQuantity_unitCode,'C174.6314':ReleaseItemGoodsQuantity})
    #~Looping End
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'CNT','C270.6069':'11','C270.6066':ReleaseAcceptanceGoodsPackagingQuantity})
    out.put({'BOTSID':'UNH'},{'BOTSID':'CNT','C270.6069':'11','C270.6066':ReleaseAcceptanceGoodsPackagingQuantity,'C270.6411':ReleaseAcceptanceGoodsPackagingTypeCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'CNT','C270.6069':'7','C270.6066':ReleaseAcceptanceMeasure,'C270.6411':ReleaseAcceptanceMeasure_measureUnitCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
